//
//  SCMaintenAlertTexttField.m
//  Shuaiche
//
//  Created by liyanan on 2019/10/24.
//  Copyright © 2019 ShuaiChe. All rights reserved.
//

#import "SCMaintenAlertTexttField.h"
#import "NSString+Helper.h"
#import "SCVinScanViewController.h"
#import "UIView+HXExtension.h"
@interface SCMaintenAlertTexttField()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *limitLab;
@property (nonatomic, strong) UIButton *scanBtn;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, copy) void(^valueChangeBlock)(NSString *);
@end
@implementation SCMaintenAlertTexttField
- (instancetype)initWithTitle:(NSString *)title
    message:(NSString *)message
    mainAct:(NSString *)main
   otherAct:(NSString *)other
                  handleBlock:(void(^)(NSInteger index))block textChanged:(void(^)(NSString *))valueChangeBlock {
    self = [self initWithTitle:title message:message tip:nil mainAct:main otherAct:other handleBlock:block];
    if (self) {
        WEAKSELF
            [self addTextFieldWithMessage:message configurationHandler:^(UITextField *textField, UILabel *messageLab) {
                weakSelf.valueChangeBlock = valueChangeBlock;
                textField.placeholder = @"请输入17位VIN码";
                textField.layer.borderColor = SC_Color_Separator.CGColor;
                textField.layer.borderWidth = 0.5;
                textField.layer.cornerRadius = 4;
                textField.keyboardType = UIKeyboardTypeASCIICapable;
                textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 40)];
                textField.leftViewMode = UITextFieldViewModeAlways;
                textField.delegate = weakSelf;
                [textField addTarget:weakSelf action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
                weakSelf.limitLab.text = @"0/17";
                UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
                [rightView addSubview:weakSelf.limitLab];
                [rightView addSubview:weakSelf.scanBtn];
                textField.rightView = rightView;
                weakSelf.textField = textField;
                textField.rightViewMode = UITextFieldViewModeAlways;
                messageLab.textColor = SC_TextBlack_Third;
                messageLab.numberOfLines = 0;
            } alwaysShow:YES];
    }
    return self;
}


//- (void)addMessage:(NSString *)message textChanged:(void(^)(NSString *))valueChangeBlock{
//    WEAKSELF
//    [self addTextFieldWithMessage:message configurationHandler:^(UITextField *textField, UILabel *messageLab) {
//        weakSelf.valueChangeBlock = valueChangeBlock;
//        textField.placeholder = @"请输入17位VIN码";
////        textField.placeholder = @"vd";
//        textField.layer.borderColor = SC_Color_Separator.CGColor;
//        textField.layer.borderWidth = 0.5;
//        textField.layer.cornerRadius = 4;
//        textField.keyboardType = UIKeyboardTypeASCIICapable;
//        textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 40)];
//        textField.leftViewMode = UITextFieldViewModeAlways;
//        textField.delegate = weakSelf;
//        [textField addTarget:weakSelf action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
//        weakSelf.limitLab.text = @"0/17";
//        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
//        [rightView addSubview:weakSelf.limitLab];
//        [rightView addSubview:weakSelf.scanBtn];
//        textField.rightView = rightView;
//        weakSelf.textField = textField;
//        textField.rightViewMode = UITextFieldViewModeAlways;
//        messageLab.textColor = SC_TextBlack_Third;
//        messageLab.numberOfLines = 0;
//    } alwaysShow:YES];
//}

- (UILabel *)limitLab {
    if (!_limitLab) {
        _limitLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
        _limitLab.font = SC_FONT(12);
        _limitLab.textColor = SC_TextBlack_Third;
        _limitLab.text = @"0/17";
    }
    return _limitLab;
}

- (UIButton *)scanBtn {
    if (!_scanBtn) {
        _scanBtn = [[UIButton alloc] initWithFrame:CGRectMake(65-30, 0, 20, 40)];
        [_scanBtn setImage:[UIImage imageNamed:@"vin_scan"] forState:0];
        [_scanBtn addTarget:self action:@selector(scanVin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}

- (void)textValueChanged:(UITextField *)textFiled {
    if (![NSString SC_isBlankString:textFiled.text]) {
        textFiled.text = [textFiled.text uppercaseString];
        if (self.valueChangeBlock) {
            self.valueChangeBlock(_textField.text);
        }
    }
    _limitLab.text = [NSString stringWithFormat:@"%zd/17",textFiled.text.length];
    
}
- (void)scanVin {
    [_textField resignFirstResponder];
    SCVinScanViewController *scan = [[SCVinScanViewController alloc] init];
    [[self viewController].navigationController pushViewController:scan animated:YES];
    scan.resultBlock = ^(NSString * string) {
        _textField.text = string;
        [_textField becomeFirstResponder];
        _limitLab.text = [NSString stringWithFormat:@"%zd/17",_textField.text.length];
        if (self.valueChangeBlock) {
            self.valueChangeBlock(_textField.text);
        }
        [[self viewController].navigationController popViewControllerAnimated:YES];
    };
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.text = [textField.text uppercaseString];
    if (self.valueChangeBlock) {
        self.valueChangeBlock(textField.text);
    }
    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        _limitLab.text = [NSString stringWithFormat:@"%zd/17",textField.text.length-1];
        return YES;
    }
    int asciiCode = [string characterAtIndex:0];
    if ((asciiCode >= 65 && asciiCode <= 90) ||(asciiCode >= 48 && asciiCode <=57) || (asciiCode >= 97 && asciiCode <= 122)) { //限制输入数字
        if (textField.text.length + string.length > 17) {
            return NO;
        }
        textField.text = [textField.text uppercaseString];
        _limitLab.text = [NSString stringWithFormat:@"%zd/17",textField.text.length+1];
        return YES;
    }
    return NO;
}
@end
